package cn.iocoder.yudao.module.ao.controller.admin.SysClass;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.SysClass.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.SysClass.SysClassDO;
import cn.iocoder.yudao.module.ao.service.SysClass.SysClassService;

@Tag(name = "管理后台 - 班级信息")
@RestController
@RequestMapping("/ao/sys-class")
@Validated
public class SysClassController {

    @Resource
    private SysClassService sysClassService;

    @PostMapping("/create")
    @Operation(summary = "创建班级信息")
    @PreAuthorize("@ss.hasPermission('ao:sys-class:create')")
    public CommonResult<Long> createSysClass(@Valid @RequestBody SysClassSaveReqVO createReqVO) {
        return success(sysClassService.createSysClass(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级信息")
    @PreAuthorize("@ss.hasPermission('ao:sys-class:update')")
    public CommonResult<Boolean> updateSysClass(@Valid @RequestBody SysClassSaveReqVO updateReqVO) {
        sysClassService.updateSysClass(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ao:sys-class:delete')")
    public CommonResult<Boolean> deleteSysClass(@RequestParam("id") Long id) {
        sysClassService.deleteSysClass(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级信息")
                @PreAuthorize("@ss.hasPermission('ao:sys-class:delete')")
    public CommonResult<Boolean> deleteSysClassList(@RequestParam("ids") List<Long> ids) {
        sysClassService.deleteSysClassListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:sys-class:query')")
    public CommonResult<SysClassRespVO> getSysClass(@RequestParam("id") Long id) {
        SysClassDO sysClass = sysClassService.getSysClass(id);
        return success(BeanUtils.toBean(sysClass, SysClassRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级信息分页")
    @PreAuthorize("@ss.hasPermission('ao:sys-class:query')")
    public CommonResult<PageResult<SysClassRespVO>> getSysClassPage(@Valid SysClassPageReqVO pageReqVO) {
        PageResult<SysClassDO> pageResult = sysClassService.getSysClassPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SysClassRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级信息 Excel")
    @PreAuthorize("@ss.hasPermission('ao:sys-class:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSysClassExcel(@Valid SysClassPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SysClassDO> list = sysClassService.getSysClassPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级信息.xls", "数据", SysClassRespVO.class,
                        BeanUtils.toBean(list, SysClassRespVO.class));
    }

}